package com.coursehub.course.repository;

import com.coursehub.course.entity.Chapter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 章节数据访问层
 */
@Repository
public interface ChapterRepository extends JpaRepository<Chapter, String> {
    
    /**
     * 根据课程ID查找章节
     */
    List<Chapter> findByCourse_CourseId(String courseId);
    
    /**
     * 根据课程ID查找章节ID列表
     */
    @Query("SELECT c.chapterId FROM Chapter c WHERE c.course.courseId = :courseId")
    List<String> findChapterIdsByCourseId(@Param("courseId") String courseId);
    
    /**
     * 根据章节名称模糊查询
     */
    List<Chapter> findByChapterNameContainingIgnoreCase(String chapterName);
    
    /**
     * 检查章节是否存在
     */
    boolean existsByChapterId(String chapterId);
    
    /**
     * 检查课程下是否存在指定名称的章节
     */
    boolean existsByCourse_CourseIdAndChapterName(String courseId, String chapterName);
}